package cn.otrue.patienthealthmanager.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.adapter.FindDoctorSouSUOAdpater;
import cn.otrue.patienthealthmanager.bean.DoctorBaseinfo;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.view.XListView;
import cn.otrue.patienthealthmanager.view.XListView.IXListViewListener;

import com.google.gson.reflect.TypeToken;

/**
 * 搜索
 * 
 * @author G
 * 
 */
public class DactorSouSuoActivity extends BaseActivity implements
		IXListViewListener {
	private int currentPage = 1;
	private List<DoctorBaseinfo> allDoctor;
	private XListView lw_all;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dactor_sousuo);

		initView();
	}

	private void initView() {

		allDoctor = new ArrayList<DoctorBaseinfo>();
		et_sousuo = (EditText) findViewById(R.id.et_sousuo);
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_back.setOnClickListener(this);
		et_sousuoshijian = (TextView) findViewById(R.id.et_sousuoshijian);
		et_sousuoshijian.setOnClickListener(this);
		lw_all = (XListView) findViewById(R.id.lw_all);
		lw_all.setPullLoadEnable(false); // 是否开启上拉加载
		lw_all.setXListViewListener(this);// 上啦加载和下拉刷新的监听
		lw_all.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				Intent intent = new Intent();
				intent.putExtra("aliasAccount", allDoctor.get(0)
						.getAliasAccount());
				intent.setClass(DactorSouSuoActivity.this,
						DoctorParticularsActivity.class);
				startActivity(intent);

			}
		});
		adpater = new FindDoctorSouSUOAdpater(DactorSouSuoActivity.this,
				allDoctor);

		lw_all.setAdapter(adpater);
	}

	private Handler handler = new ServerHandler() {

		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			onLoad();
			if (state.equals("001")) {
				String dataStr = data.getString("data");
				List<DoctorBaseinfo> newList = HttpServer.gson.fromJson(
						dataStr, new TypeToken<List<DoctorBaseinfo>>() {
						}.getType());

				allDoctor.addAll(newList);
				if (allDoctor.size()>5) {
					lw_all.setPullLoadEnable(true);
				}
				adpater = new FindDoctorSouSUOAdpater(
						DactorSouSuoActivity.this, allDoctor);
				Toast.makeText(DactorSouSuoActivity.this, msg, 0).show();
				lw_all.setAdapter(adpater);

			} else {
				onLoad();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			onLoad();
		}

	};
	private FindDoctorSouSUOAdpater adpater;
	private EditText et_sousuo;
	private TextView tv_back;
	private TextView et_sousuoshijian;

	private void onLoad() {
		lw_all.stopRefresh();
		lw_all.stopLoadMore();
		lw_all.setRefreshTime("刚刚");
	}

	private void getData(int i) {

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("mobile", "");
		map.put("realname", et_sousuo.getText().toString());

		TransferParameter parameter = new TransferParameter();
		parameter.setPagesize("50");
		parameter.setCurrentPage(i + "");
		parameter.setData(map);
		new HttpServer().post(HttpCons.QUERY_DOCTOR_LIST, parameter, handler);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_back:
			finish();
			break;
		case R.id.et_sousuoshijian:
			if (et_sousuoshijian.getText().toString() != null) {
				allDoctor.clear();
				getData(currentPage);
			} else {

			}

			break;
		default:
			break;
		}
	}

	@Override
	public void onRefresh() {
		allDoctor.clear();
		currentPage = 1;

		getData(currentPage);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		currentPage++;

		getData(currentPage);
	}

}
